/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;
import Model.*;
import javax.microedition.lcdui.*;
/**
 *
 * @author Pisarchik_S
 */
public class EditRecord implements CommandListener , ItemCommandListener {
    Dispatcher m_Dispatcher;
    CModel m_Model;

    public EditRecord(Dispatcher dispatcher, CModel model)
    {
        m_Dispatcher = dispatcher;
        m_Model = model;
    }

    StringItem m_sValue;
    ChoiceGroup  m_Categories;
    ChoiceGroup m_Currency;
    int m_curCategory;

    Command m_CancelCommand = new Command("Скасаваць", Command.CANCEL, 0);
    Command m_OkCommand = new Command("Ужыць", Command.OK, 0);

    CRecord m_EditedRecord;

    TextField m_money;
    DateField m_Date;
    TextField m_note;

    StringItem m_DelButton;
    Command m_DelCommand = new Command("Выдаліць", Command.ITEM, 0);

    public void Init(CRecord record)
    {
        m_EditedRecord = record;

        Form form = new Form("Рэдагаваць запіс");
        m_money = new TextField("Колькасць грошай", Integer.toString(record.m_nAmount), 12, TextField.NUMERIC);
        m_money.setDefaultCommand(m_OkCommand);
        m_money.setItemCommandListener(this);
        form.append(m_money);

        String sCateg[];
        sCateg = new String[m_Model.m_Categories.length];
        for (int  i=0; i< m_Model.m_Categories.length; ++i)
        {
            sCateg[i] = m_Model.m_Categories[i].GetName();
            if (m_Model.m_Categories[i] == record.m_category)
                m_curCategory = i;
        }

        m_Categories = new ChoiceGroup ("Катэгорыя", ChoiceGroup.POPUP, sCateg, null );
        m_Categories.setSelectedIndex(m_curCategory, true);
        m_Categories.setDefaultCommand(m_OkCommand);
        m_Categories.setItemCommandListener(this);
        form.append(m_Categories);

        m_note = new TextField("Нататка: ",record.m_Notes, 1024, TextField.ANY);
        m_note.setDefaultCommand(m_OkCommand);
        m_note.setItemCommandListener(this);
        form.append(m_note);

        m_Date = new DateField("Дата", DateField.DATE_TIME);
        m_Date.setDate(record.m_date);
        m_Date.setDefaultCommand(m_OkCommand);
        m_Date.setItemCommandListener(this);
        form.append(m_Date);

        m_DelButton = new StringItem("", "Выдаліць запіс", StringItem.BUTTON);
        m_DelButton.setDefaultCommand(m_DelCommand);
        m_DelButton.setItemCommandListener(this);
        form.append(m_DelButton);


        form.addCommand(m_CancelCommand);
        form.setCommandListener(this);

        m_Dispatcher.GetDisplay().setCurrent(form);
    }

    public void commandAction(Command c, Displayable s)
    {
        if (c == m_CancelCommand)
        {
            m_Dispatcher.GetShowMonth().Init();
        }
    }

    public void commandAction(Command c, Item item)
    {
        if (c == m_DelCommand)
        {
            m_Model.RemoveRecord(m_EditedRecord);
            m_Dispatcher.GetShowMonth().Init();
        }
        if (c == m_OkCommand)
        {
            //CCurrency cur = m_model.m_Curencies[m_Currency.getSelectedIndex()];
            m_EditedRecord.m_currency = m_Model.m_Curencies[0];
            m_EditedRecord.m_category = m_Model.m_Categories[m_Categories.getSelectedIndex()];
            if (m_money.getString().length() == 0)
                return;
            m_EditedRecord.m_nAmount = Integer.parseInt(m_money.getString());
            m_EditedRecord.m_date = m_Date.getDate();
            m_EditedRecord.m_Notes = m_note.getString();
            m_Model.SortRecords();
            m_Dispatcher.GetShowMonth().Init();
        }
    }
}
